<template>
  <div>
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <div style="margin:20px 0;">
        <span style="font-size:35px;font-weight:600;">文章编辑</span>
      </div>
      <div style="margin:10px 0;">
        <span style="font-size:25px;color:red;">(上下拖动简介、段落文本框右下角改变高度)</span>
      </div>
      <div style="margin:10px 0;">
        <span style="font-size:25px;color:red;">(段落换行直接回车换行即可，不需要考虑每段前空两个字符)</span>
      </div>
      <el-form-item prop="title" label="标题">
        <el-input style="font-size:18px;" v-model="dynamicValidateForm.title"></el-input>
      </el-form-item>
      <el-form-item label="简介" :prop="dynamicValidateForm.introduction">
        <el-input :autosize="{ minRows: 2}" style="font-size:18px;" type="textarea"
          v-model="dynamicValidateForm.introduction"></el-input>
      </el-form-item>
      <el-form-item label="内容" :prop="dynamicValidateForm.content">
        <el-input :autosize="{ minRows: 10}" style="font-size:18px;" type="textarea"
          v-model="dynamicValidateForm.content"></el-input>
      </el-form-item>

      <div style="margin:20px 0;">
        <span style="font-size:35px;font-weight:600;">选择上传配图(点击图片进行移除)</span>
      </div>
      <div style="margin:10px 0;">
        <span style="font-size:25px;color:red;">(谨慎选择图片，提交后暂不支持修改图片)</span>
      </div>

      <div class="imgItem">
        <div v-for="(item,index) in imgList" :key="index">
          <el-image class="img" fit="cover" :src="item.url" title="点击移除图片" @click="deleteimg(index)">
          </el-image>
          <!-- <el-form-item label="图片标题">
            <el-input v-model="item.name" class="ipt">
            </el-input>
          </el-form-item> -->
        </div>
      </div>
      <el-upload :multiple='true' action="#" list-type="picture-card" :show-file-list='false' :auto-upload="false"
        :on-change="changefile">
        <i slot="default" class="el-icon-plus">选择</i>
      </el-upload>
      <div style="margin:15px 0;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'News',
  props: {
    id: String
  },
  data () {
    return {
      dynamicValidateForm: {
        id: this.id,
        title: '',
        introduction: '',
        content: ''
      },
      Form: {
        id: this.id,
        title: '',
        introduction: '',
        content: ''
      },
      imgList: []
    }
  },
  methods: {
    submitForm (formName) {
      if (this.dynamicValidateForm.title && this.dynamicValidateForm.introduction && this.dynamicValidateForm.content) {
        this.Form = this.dynamicValidateForm
        var text = this.dynamicValidateForm.content
        var re = /\n/g
        var string = text.replace(re, '<span>')
        this.Form.content = string
        console.log(this.Form)
        var formData = new FormData()
        let that = this
        formData.append('content', encodeURI(that.Form.content))
        formData.append('introduction', that.Form.introduction)
        formData.append('title', that.Form.title)
        for (let index = 0; index < that.imgList.length; index++) {
          formData.append('imgs', that.imgList[index].raw)
        }
        this.$axios({
          method: 'post',
          url: '/success/insert',
          data: formData
        }).then((res) => {
          if (res.status === 200) {
            console.log(res)
          }
        })
      }
    },
    open (text) {
      this.$notify({
        title: '成功',
        message: text,
        type: 'success'
      })
    },
    changefile (file, fileList) {
      this.imgList = fileList
    },
    deleteimg (index) {
      this.imgList.splice(index, 1)
    }
  }
}
</script>

<style scoped>
.imgItem {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  overflow: hidden;
  margin: 15px 0;
}
.img {
  width: 450px;
  height: 300px;
}
.ipt {
  width: 350px;
}
</style>
